package com.sright.bcard.mini.web.controller;

import com.sright.bcard.common.service.share.dto.request.AccessTimeRequest;
import com.sright.bcard.common.service.share.dto.request.VisitorPageRequest;
import com.sright.bcard.mini.web.constant.WebUrl;
import com.sright.bcard.user.share.request.AddVisitorParamRequest;
import com.sright.bcard.user.share.request.TokenParam;
import com.sright.bcard.user.share.service.VisitorService;
import com.sright.common.response.SrightResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 名片访问记录controller
 */
@RestController
@RequestMapping()
public class CardVisitorController {

    @Autowired
    private VisitorService visitorService;

    /**
     * 记录名片访问者信息
     * @param addVisitorParamRequest
     * @param tokenParam
     * @return
     */
    @PostMapping(WebUrl.VisitorRecord)
    public SrightResponse reportVisitor(@RequestBody AddVisitorParamRequest addVisitorParamRequest, @RequestAttribute TokenParam tokenParam, HttpServletRequest request) throws Exception {
        buildVisitorContext(addVisitorParamRequest,tokenParam);
        return visitorService.recordVisitor(addVisitorParamRequest,request);
    }

    /**
     * 上报访问时间
     * @return
     */
    @PostMapping(WebUrl.RecordAccessTime)
    public SrightResponse accessTime(@RequestBody AccessTimeRequest accessTimeRequest) {
        return visitorService.recordAccessTime(accessTimeRequest.getRecordId(), accessTimeRequest.getTime());
    }

    /**
     * 获取访客数据
     * @return
     */
    @GetMapping(WebUrl.VisitorRecord)
    public SrightResponse getVisitorData(VisitorPageRequest visitorPageRequest) {
        return visitorService.pageVisitorList(visitorPageRequest);
    }

    private AddVisitorParamRequest buildVisitorContext(AddVisitorParamRequest addVisitorParamRequest, TokenParam tokenParam) {
        addVisitorParamRequest.setMemberId(tokenParam.getMemberId());
        addVisitorParamRequest.setVisitorEnterpriseUserId(tokenParam.getEnterpriseUserId());
        addVisitorParamRequest.setUserId(tokenParam.getPersonalUserId());
        addVisitorParamRequest.setEnterpriseId(tokenParam.getEnterpriseId());
        return addVisitorParamRequest;
    }



}
